package com.file.manage.infrastructure.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: dzg
 * @Date: 2023/9/19 15:46
 * @Describe: 美化界面地址，/{applicationName}/doc.html
 * 来自：https://doc.xiaominfo.com/docs/quick-start
 */
@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI customOpenApi() {
        return new OpenAPI()
                .info(new Info()
                        .title("文件管理系统API")
                        .version("1.0")
                        // 作者
                        .contact(new Contact().name("H41"))
                        // 描述
                        .description("文件管理系统Api接口文档"))
                .components(new Components()
                        .addSecuritySchemes("Authorization",
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.HTTP)
                                        .scheme("token")
                                        .bearerFormat("")));
    }

    @Bean
    public GlobalOpenApiCustomizer globalOpenApiCustomizer() {
        return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream()).forEach(operation -> operation.security(openApi.getSecurity()));
    }

    /**
     * Yml配置分组示例
     * springdoc:
     * group-configs:
     * - group: 'dzg-group1'
     * paths-to-match: '/**'
     * packages-to-scan: com.dzg.demo.controller.group1
     * - group: 'dzg-group2'
     * paths-to-match: '/**'
     * packages-to-scan: com.dzg.demo.controller.group2
     *
     * @return
     */
    @Bean
    public GroupedOpenApi dzgGroup1() {
        return GroupedOpenApi.builder()
                .packagesToScan("com.file.manage.controller")
                .group("group1")
                .pathsToMatch("/**")
                .build();
    }
}


